package com.kfm.base.string;

public class Exer03 {

    public static void main(String[] args) {
        String str = "((()()()";
        boolean result = isTrue(str);
        System.out.println(result);
    }

    private static boolean isTrue(String str) {
        String s = "()";
        int length = -1;
        if (str.length() % 2 != 0) {
            return false;
        }
        // str.length() >= 2 &&
        while (str.length() != length) {
            length = str.length(); // 记录替换之前的长度
            str = str.replace(s, "");
        }

        return str.length() == 0;
    }

    private static boolean isTrue1(String str) {
        if (str.length() % 2 != 0) {
            return false;
        }
        int leftCount = 0;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c == '(') {
                leftCount++;
            } else if (c == ')') {
                leftCount--;
                if (leftCount < 0) {
                    return false;
                }
            }
        }
        return leftCount == 0;
    }

}

